package com.ruoyi.web.controller.asset;

import java.util.List;

import com.ruoyi.asset.domain.vo.MaintenanceStatementVO;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.framework.web.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.MaintenanceRecord;
import com.ruoyi.asset.service.IMaintenanceRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修保养记录Controller
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Controller
@RequestMapping("/asset/maintenanceRecord")
public class MaintenanceRecordController extends BaseController {
    private String prefix = "asset/maintenanceRecord";

    @Autowired
    private IMaintenanceRecordService maintenanceRecordService;


    @Autowired
    private DictService dictService;




    @RequiresPermissions("asset:maintenanceRecord:view")
    @GetMapping()
    public String maintenanceRecord() {
        return prefix + "/maintenanceRecord";
    }

    /**
     * 查询维修保养记录列表
     */
    @RequiresPermissions("asset:maintenanceRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaintenanceRecord maintenanceRecord) {
        startPage();
        List<MaintenanceRecord> list = maintenanceRecordService.selectMaintenanceRecordList(maintenanceRecord);
        return getDataTable(list);
    }

    /**
     * 导出维修保养记录列表
     */
    @RequiresPermissions("asset:maintenanceRecord:export")
    @Log(title = "维修保养记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaintenanceRecord maintenanceRecord) {
        List<MaintenanceRecord> list = maintenanceRecordService.selectMaintenanceRecordList(maintenanceRecord);
        ExcelUtil<MaintenanceRecord> util = new ExcelUtil<MaintenanceRecord>(MaintenanceRecord.class);
        return util.exportExcel(list, "维修保养记录数据");
    }

    /**
     * 新增维修保养记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存维修保养记录
     */
    @RequiresPermissions("asset:maintenanceRecord:add")
    @Log(title = "维修保养记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaintenanceRecord maintenanceRecord) {
        return toAjax(maintenanceRecordService.insertMaintenanceRecord(maintenanceRecord));
    }

    /**
     * 修改维修保养记录
     */
    @RequiresPermissions("asset:maintenanceRecord:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        MaintenanceRecord maintenanceRecord = maintenanceRecordService.selectMaintenanceRecordById(id);
        mmap.put("maintenanceRecord", maintenanceRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存维修保养记录
     */
    @RequiresPermissions("asset:maintenanceRecord:edit")
    @Log(title = "维修保养记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaintenanceRecord maintenanceRecord) {
        return toAjax(maintenanceRecordService.updateMaintenanceRecord(maintenanceRecord));
    }

    /**
     * 取消维修保养
     */
    @RequiresPermissions("asset:maintenanceRecord:cancel")
    @Log(title = "维修保养取消", businessType = BusinessType.OTHER)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(String ids) {
        return toAjax(maintenanceRecordService.deleteMaintenanceRecordByIds(ids));
    }


    /**
     * 批量取消维修保养状态
     */
    @RequiresPermissions("asset:maintenanceRecord:cancelById")
    @Log(title = "维修保养批量取消", businessType = BusinessType.OTHER)
    @PostMapping("/cancelById")
    @ResponseBody
    public AjaxResult cancelByIdSave(String id) {
        return toAjax(maintenanceRecordService.deleteMaintenanceRecordById(id));
    }


    @RequiresPermissions("asset:maintenanceRecord:statement")
    @GetMapping("/maintenanceStatement")
    public String maintenanceStatement() {
        return prefix + "/maintenanceStatement";
    }

    @RequiresPermissions("asset:maintenanceRecord:statementList")
    @PostMapping("/initMaintenanceStatement")
    @ResponseBody
    public AjaxResult initMaintenanceStatement(MaintenanceRecord maintenanceRecord) {
        List<MaintenanceStatementVO> maintenanceStatementVOS = maintenanceRecordService.selectMaintenanceStatement(maintenanceRecord);

        List<SysDictData> maintenanceContents = dictService.getType("maintenance_content");

        maintenanceStatementVOS.forEach(maintenanceStatement -> {
            maintenanceContents.forEach(maintenanceContent -> {
                        if (maintenanceStatement.getMaintenanceContent().equals(maintenanceContent.getDictValue())) {

                            maintenanceStatement.setMaintenanceContent(maintenanceContent.getDictLabel());
                        }
                    }
            );
        });


        return new AjaxResult(AjaxResult.Type.SUCCESS, "成功", maintenanceStatementVOS);
    }


}
